import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;


@SuppressWarnings("serial")
public class Board implements Serializable {
	final private BoardCell[][] board;
	final private int width;
	final private int height;
	
	public Board(int height, int width)
	{
		this.width = width;
		this.height = height;
		board = new BoardCell[height][width];
	}
	
	public String createPattern(int fromx, int tox, int fromy, int toy)
	{
		if(fromx != tox && fromy != toy) return null;
		
		if(fromx == tox)	//poziomy
		{
			StringBuilder pattern = new StringBuilder();
			int x = tox;
			int actualIndex = fromy;
			int lastLetterIndex = toy;
			while(board[x][lastLetterIndex] == null) lastLetterIndex--;
			while(board[x][actualIndex] == null)
			{
				pattern.append(".?");
				actualIndex++;
			}
			while(actualIndex != lastLetterIndex)
			{
				if (board[x][actualIndex] == null)
					pattern.append(".");
				else
					pattern.append(board[x][actualIndex].getContent());
				actualIndex++;
			}
			pattern.append(board[x][lastLetterIndex].getContent());
			for(int i = actualIndex; i < toy; i++)
			{
				pattern.append(".?");
			}
			return pattern.toString();
		}
		
		if(fromy == toy)	//pionowy
		{
			StringBuilder pattern = new StringBuilder();
			int y = toy;
			int actualIndex = fromx;
			int lastLetterIndex = tox;
			while(board[lastLetterIndex][y] == null) lastLetterIndex--;
			while(board[actualIndex][y] == null)
			{
				pattern.append(".?");
				actualIndex++;
			}
			while(actualIndex != lastLetterIndex)
			{
				if (board[actualIndex][y] == null)
					pattern.append(".");
				else
					pattern.append(board[actualIndex][y].getContent());
				actualIndex++;
			}
			pattern.append(board[lastLetterIndex][y].getContent());
			for(int i = actualIndex; i < tox; i++)
			{
				pattern.append(".?");
			}
			return pattern.toString();
		}
		
		return null;
	}
	
	public int getWidth() {
		return width;
	}
	
	public int getHeight() {
		return height;
	}
	
	public BoardCell getCell(int x, int y)
	{
		return board[x][y];
	}
	
	public void setCell(int x, int y, BoardCell c)
	{
		board[x][y] = c;
	}
	
	public LinkedList<BoardCell> getStartCells()
	{
		LinkedList<BoardCell> result = new LinkedList<BoardCell>();
		for(int i=0; i < height; i++)
		{
			for(int j=0; j < width; j++)
			{
				if (board[i][j].isStart())
					result.add(board[i][j]);
			}
				
		}
		return result;
	}
	
	public ArrayList<ComplexBoardCell> getPossibleCells()
	{
		ArrayList<ComplexBoardCell> result = new ArrayList<ComplexBoardCell>();
		for(int i=0; i < height; i++)
		{
			for(int j=0; j < width; j++)
			{
				if (board[i][j] != null && !board[i][j].isDisabled())
					result.add(new ComplexBoardCell(board[i][j],i,j));
			}
				
		}
		return result;
	}
	
	public void printPossibles()
	{
		System.out.println();
		for(int i=0; i < height; i++)
		{
			for(int j=0; j < width; j++)
			{
				if (board[i][j] != null && !board[i][j].isDisabled())
					System.out.print("O");
				else
					System.out.print("*");
			}
			System.out.println();	
		}
		System.out.println();
	}
	
	public void print()
	{
		for(int i=0; i < height; i++)
		{
			for(int j=0; j < width; j++)
			{
				if(board[i][j] == null)
					System.out.print("*");
				else
					System.out.print(board[i][j].getContent());
			}
			System.out.println();
		}
	}
}
